/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.MemoriaInterface;
import br.gov.cemaden.library.model.smp.ExecHardware;
import br.gov.cemaden.library.model.smp.Memoria;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class MemoriaDao extends GenericDao implements MemoriaInterface {

    private Session session;

    public MemoriaDao(Session session) {
        this.session = session;
    }

    public List<Memoria> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Memoria> memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findAll");
            memoria = (List<Memoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public Memoria findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Memoria memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findById").setParameter("id", id);
            memoria = (Memoria) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public List<Memoria> findByMemoryLivreTotalReal(Double memoryLivreTotalReal) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Memoria> memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findByMemoryLivreTotalReal").setParameter("memoryLivreTotalReal", memoryLivreTotalReal);
            memoria = (List<Memoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findByMemoryLivreTotalReal: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public List<Memoria> findByMemoryUsadaTotalReal(Double memoryUsadaTotalReal) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Memoria> memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findByMemoryUsadaTotalReal").setParameter("memoryUsadaTotalReal", memoryUsadaTotalReal);
            memoria = (List<Memoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findByMemoryUsadaTotalReal: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public List<Memoria> findByMemoryTotalUtilizado(Double memoryTotalUtilizado) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Memoria> memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findByMemoryTotalUtilizado").setParameter("memoryTotalUtilizado", memoryTotalUtilizado);
            memoria = (List<Memoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findByMemoryTotalUtilizado: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public List<Memoria> findByMemoryTotalLivre(Double memoryTotalLivre) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Memoria> memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findByMemoryTotalLivre").setParameter("memoryTotalLivre", memoryTotalLivre);
            memoria = (List<Memoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findByMemoryTotalLivre: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public List<Memoria> findByMemoryRam(Double memoryRam) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Memoria> memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findByMemoryRam").setParameter("memoryRam", memoryRam);
            memoria = (List<Memoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findByMemoryRam: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public List<Memoria> findByMemoryTotal(Double memoryTotal) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Memoria> memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findByMemoryTotal").setParameter("memoryTotal", memoryTotal);
            memoria = (List<Memoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findByMemoryTotal: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public List<Memoria> findByExecHardware(ExecHardware execHardware) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Memoria> memoria = null;
        try {
            Query query = session.getNamedQuery("Memoria.findByExecHardware").setParameter("execHardware", execHardware);
            memoria = (List<Memoria>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no MemoriaFacade.findByExecHardware: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return memoria;
    }

    public Memoria insert(Memoria memoria) {
        try {
            saveOrUpdate(this.session, memoria);
        } catch (Exception ex) {
            Logger.getLogger(MemoriaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return memoria;
    }

    public Memoria update(Memoria memoria) {
        try {
            saveOrUpdate(this.session, memoria);
        } catch (Exception ex) {
            Logger.getLogger(MemoriaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return memoria;
    }

    public void delete(Memoria memoria) {
        try {
            remove(this.session, memoria);
        } catch (Exception ex) {
            Logger.getLogger(MemoriaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
